<?php

namespace App\Services;

use App;
use App\Models\Category;
use App\Models\Project;
use App\Models\News;

class SitemapService {

    public function buildHome()
    {
        $sitemap = App::make("sitemap");
        $sitemap->add(env('PC_URL'), date(DATE_RFC3339, time()), '1.0', 'daily');
        $info = $sitemap->store('xml', 'home', public_path('sitemap'));
        \Log::info($info);
        return true;
    }

    public function buildCategories()
    {
        $sitemap = App::make("sitemap");
        $lastModTime = 0;
        Category::chunk(100, function ($categories) use ($sitemap, &$lastModTime) {
            foreach ($categories as $category) {
                $catLastModTime = strtotime($category->updated_at);
                if ($catLastModTime > $lastModTime) {
                    $lastModTime = $catLastModTime;
                }
                if ($category->parent_id == 0) {
                    $url = env('PC_URL') . "/project/type" . $category->id;
                } else {
                    $url = env('PC_URL') . "/project/type" . $category->parent_id . '-' . $category->id;
                }
                $sitemap->add($url, date(DATE_RFC3339, strtotime($category->updated_at)), '0.5', 'weekly');
            }
        });
        $info = $sitemap->store('xml','categories', public_path('sitemap'));
        \Log::info($info);
        return $lastModTime;
    }

    public function buildNewsType() {
        $sitemap = App::make("sitemap");
        $lastModTime = 0;
        $list = App\Models\Config::where('key', 'news_type')->get();
        foreach ($list as $item) {
            $catLastModTime = strtotime($item->updated_at);
            if ($catLastModTime > $lastModTime) {
                $lastModTime = $catLastModTime;
            }
            $url = env('PC_URL') . "/news/child/" . $item->id;
            $sitemap->add($url, date(DATE_RFC3339, strtotime($item->updated_at)), '0.8', 'weekly');
        }
        $info = $sitemap->store('xml','news_type', public_path('sitemap'));
        \Log::info($info);
        return $lastModTime;
    }

    public function buildProject() {
        $sitemap = App::make("sitemap");
        $lastModTime = 0;
        Project::orderBy('id', 'desc')->chunk(100, function ($list) use ($sitemap, &$lastModTime) {
            foreach ($list as $item) {
                $catLastModTime = strtotime($item->updated_at);
                if ($catLastModTime > $lastModTime) {
                    $lastModTime = $catLastModTime;
                }
                $url = env('PC_URL') . "/project/detail/" . $item->id;
                $sitemap->add($url, date(DATE_RFC3339, strtotime($item->updated_at)), '0.8', 'weekly');
            }
        });
        $info = $sitemap->store('xml','project', public_path('sitemap'));
        \Log::info($info);
        return $lastModTime;
    }

    public function buildNews() {
        $sitemap = App::make("sitemap");
        $lastModTime = 0;
        News::orderBy('id', 'desc')->chunk(100, function ($list) use ($sitemap, &$lastModTime) {
            foreach ($list as $item) {
                $catLastModTime = strtotime($item->updated_at);
                if ($catLastModTime > $lastModTime) {
                    $lastModTime = $catLastModTime;
                }
                $url = env('PC_URL') . "/news/detail/" . $item->id;
                $sitemap->add($url, date(DATE_RFC3339, strtotime($item->updated_at)), '0.8', 'weekly');
            }
        });
        $info = $sitemap->store('xml','news', public_path('sitemap'));
        \Log::info($info);
        return $lastModTime;
    }



    public function buildIndex()
    {
        // create sitemap index
        $sitemap = App::make ("sitemap");

        // add sitemaps (loc, lastmod (optional))
        if ($this->buildHome()) {
            $sitemap->addSitemap(env('PC_URL') . '/sitemap/home.xml', date(DATE_RFC3339, time()));
        }

        if ($lastModTime = $this->buildCategories()) {
            $sitemap->addSitemap(env('PC_URL') . '/sitemap/categories.xml', date(DATE_RFC3339, $lastModTime));
        }

        if ($lastModTime = $this->buildProject()) {
            $sitemap->addSitemap(env('PC_URL') . '/sitemap/project.xml', date(DATE_RFC3339, $lastModTime));
        }

        if ($lastModTime = $this->buildNewsType()) {
            $sitemap->addSitemap(env('PC_URL') . '/sitemap/news_type.xml', date(DATE_RFC3339, $lastModTime));
        }

        if ($lastModTime = $this->buildNews()) {
            $sitemap->addSitemap(env('PC_URL') . '/sitemap/news.xml', date(DATE_RFC3339, $lastModTime));
        }


//        if ($lastModTimes = $this->buildDiscussions()) {
//            foreach ($lastModTimes as $name => $time) {
//                $sitemap->addSitemap(config('app.url') . '/storage/sitemap/discussions-' . $name . '.xml', date(DATE_RFC3339, $time));
//            }
//        }

        // create file sitemap.xml in your public folder (format, filename)
        $sitemap->store('sitemapindex', 'sitemap');
    }

}
